<template>
	<view>
		<view class="my-search-box">
			<MySearch></MySearch>
		</view>
		<view class="scroll-second-box">
			<!-- 左侧的滑动视图 -->
			<scroll-view scroll-y="true" class="left-scroll-list" :style="{height: wh + 'px'}">
				<block v-for="(item,i) in cataList" :key="i">
					<view :class="['left-scroll-view-item', active===i?'active':'']" @click="changeActive(i)">
						{{item.cat_name}}
					</view>
				</block>
			</scroll-view>
			<!-- 右侧的滑动视图 二级分类-->
			<scroll-view scroll-y="true" class="right-scroll-list" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
				<!-- 二级分类开始 -->
				<view class="cate-lv2-list" v-for="(item2,i2) in cateLevel2" :key="i2">
					<view class="right-scroll-view-item">
						<view class="cata-lv2-title">
							{{item2.cat_name}}
						</view>
						<!-- 三级分类开始 -->
						<view class="cate-lv3-list">
							<view class="cate-lv3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodList(item3)">
								<!-- 图片 -->
								<image :src="item3.cat_icon.replace('dev','web')" />
								<!-- 文本 -->
								<text>{{item3.cat_name}}</text>
							</view>
						</view>
						<!-- 三级分类结束 -->
					</view>
				</view>
				<!-- 二级分类结束 -->
			</scroll-view>
		</view>
	</view>
</template>

<script>
import MySearch from '../../components/my-search/my-search.vue';
	export default {
		data() {
			return {
				cataList: [],
				cateLevel2: [],
				cateLevel3: [],
				wh: 0,
				active: 0,
				scrollTop:0
			};
		},
		components:{
			MySearch
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync();
			this.wh = sysInfo.windowHeight - 50
			this.getCateList()
		},
		methods: {
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				if (res.meta.status !== 200) return uni.$showErrorMsg()
				this.cataList = res.message
				this.cateLevel2 = res.message[0].children
			},
			changeActive(i) {
				this.active = i;
				this.cateLevel2 = this.cataList[i].children;
				this.scrollTop = this.scrollTop?0:1;
			},
			gotoGoodDetail(item3){
				uni.navigateTo({
					url : '/subpkg/goods_detail/goods_detail?cid='+item3.cat_id
				})
			},
			gotoGoodList(item3){
				console.log("gotoGoodList");
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_id
				})
			}
			
		}
	}
</script>

<style lang="scss">
	.scroll-second-box {
		display: flex;

		.left-scroll-list {
			width: 120px;

			.left-scroll-view-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;

				// 激活项的样式
				&.active {
					background-color: #ffffff;
					position: relative;

					// 渲染激活项左侧的红色指示边线
					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}
		}

		.right-scroll-list{
			background-color: #ffffff;
			.right-scroll-view-item {
				display: block;
				text-align: center;
				font-size: 12px;
				.cata-lv2-title {
					font-weight: bold;
					padding: 10px 0px;
				}
				.cate-lv3-list {
					display: flex;
					flex-wrap: wrap;
			
					.cate-lv3-item {
						display: flex;
						margin-bottom: 10px;
						flex-direction: column;
						width: 33.33%;
						align-items: center;
			
						image {
							width: 60px;
							height: 60px;
						}
			
						text {
							font-size: 12px;
						}
					}
				}
			
			}
		}
		.my-serach-box{
			position: sticky;
			top: 0;
			z-index: 999;
		}
	}
</style>